package com.lkx.web;

import com.lkx.config.ImageConfig;
import com.lkx.entity.User;
import com.lkx.entity.UserData;
import com.lkx.service.UserDataService;
import com.lkx.util.FileUtil;
import com.lkx.util.IPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: UserDataController
 * Function:  TODO
 * Date:      2018-12-17 12:42
 * author     黄曦
 * version    V1.0
 */
@Controller
@RequestMapping("/userData")
public class UserDataController {

    @Autowired
    private ImageConfig imageConfig;
    @Autowired
    private UserDataService userDataService;

    @RequestMapping("toAdd")
    public ModelAndView toAdd(UserData userData,HttpSession session) {
        ModelAndView view = new ModelAndView("user/login");

        if(session.getAttribute("username") == null){
            return view;
        }
        view.addObject("username",session.getAttribute("username"));
        view.setViewName("userData/add");
        return view;
    }

    @RequestMapping("add")
    public ModelAndView add(UserData userData,HttpServletRequest request) {

        ModelAndView view = new ModelAndView("user/login");
        if(request.getSession().getAttribute("user") == null){
            return view;
        }
        User user = (User)request.getSession().getAttribute("user");
        userData.setUserId(user.getId());
        userData.setGmtCreate(new Date());
        userData.setGmtModify(new Date());
        userData.setUserIp(IPUtils.getIpAddr(request));
        userDataService.insert(userData);

        view.addObject("username",request.getSession().getAttribute("username"));
        view.setViewName("redirect:/");
        return view;
    }

    @RequestMapping("upload")
    @ResponseBody
    public UserData upload(UserData userData, @RequestParam("file") MultipartFile file,
                                     HttpServletRequest request) {

        String fileName = file.getOriginalFilename();  //文件名字
        //文件存放路径
        String filePath = imageConfig.getUploadPath();

        //调用文件处理类FileUtil，处理文件，将文件写入指定位置
        try {
            FileUtil.uploadFile(file.getBytes(), filePath, fileName);
        } catch (Exception e) {

        }

        userData.setDataUrl(fileName);
        userData.setDataName(fileName);

        return userData;
    }

    @RequestMapping("detail")
    public ModelAndView add(Long id, HttpSession session) {
        ModelAndView view = new ModelAndView("userData/detail");
        view.addObject("username",session.getAttribute("username"));

        UserData userData = userDataService.selectByPrimaryKey(id);
        userData.setDataCount(userData.getDataCount()+1);
        userData.setGmtModify(new Date());
        userDataService.updateByPrimaryKey(userData);
        view.addObject("userData",userData);
        view.addObject("id",id);

        //权限控制
        if(session.getAttribute("user") != null) {
            User user = (User) session.getAttribute("user");
            if (2==user.getUserType() || userData.getUserId().compareTo(user.getId()) == 0) {
                view.addObject("view", true);
            }
        }else{
            view.addObject("view",false);
        }
        view.addObject("dataUrl",imageConfig.getPrefix()+userData.getDataUrl());
        return view;
    }


    @RequestMapping("delete")
    public ModelAndView delete(Long id, HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        view.addObject("username",request.getSession().getAttribute("username"));
        view.setViewName("redirect:/");
        userDataService.deleteByPrimaryKey(id);
        return view;
    }
}
